package com.laomou.poker.rule.fried;

import java.util.List;

import com.laomou.poker.Poker;
import com.laomou.poker.PokerGroup;
import com.laomou.poker.rule.AbsPokerRule;
import com.laomou.poker.rule.PokerRuleType;

public class Fried extends AbsPokerRule {

    @Override
    public PokerRuleType getRuleType() {
        return PokerRuleType.炸金花;
    }

    @Override
    public boolean isRule(PokerGroup pg) {
        if (pg.size() != 3) {
            return false;
        }
        if (isSame(pg)) {
            pg.setType(FriedType.豹子);
            return true;
        }
        if (_isSameColor(pg)) {
            if (_isStraight(pg)) {
                pg.setType(FriedType.顺金);
            } else {
                pg.setType(FriedType.金花);
            }
            return true;
        } else {
            if (_isStraight(pg)) {
                pg.setType(FriedType.顺子);
                return true;
            }
        }
        if (hasSame(pg, 2)) {
            pg.setType(FriedType.对子);
            return true;
        } else {
            pg.setType(FriedType.单张);
        }
        return true;
    }

    @Override
    public boolean isGreater(PokerGroup lp, PokerGroup rp) {
        boolean IsGreater = false;
        if (lp.type() != rp.type()) {
            if (lp.type().value() > rp.type().value()) {
                IsGreater = true;
            }
        } else {
            if (lp.value() > rp.value()) {
                IsGreater = true;
            }
        }
        return IsGreater;
    }

    @Override
    public int getPlayerPokerCount() {
        return 3;
    }

    @Override
    public int getPokerCount() {
        return 1;
    }

    private boolean _isStraight(PokerGroup pokerGroup) {
        List<Poker> list = smalltobig(pokerGroup).toList();
        return isStraight(list);
    }

    private boolean _isSameColor(PokerGroup pokerGroup) {
        return hasSameColor(pokerGroup, 3);
    }

}
